package com.fit.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.BuyBean;
import com.fit.beans.OtkupBean;
import com.fit.interfaces.Buy;
import com.fit.interfaces.Customer;
import com.fit.interfaces.Jewelry;
import com.fit.interfaces.Otkup;
import com.fit.interfaces.Salesman;
import com.fit.interfaces.User;
import com.fit.service.Buys;
import com.fit.service.Customers;
import com.fit.service.Jewelrys;
import com.fit.service.Otkupi;
import com.fit.service.Salesmen;

public class EntryOtkupi {

	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		return null;
	}
	private String aee;
	@Persist
	private String otkupiCustomer;
	@Persist
	private String otkupiSalesman;
	@Persist
	private String otkupiJewelry;
	@InjectPage
	private ViewOtkupi viewOtkupiPage;
	@InjectPage
	private ViewError viewErrorPage;


	@Inject
	private Jewelrys jewelrys;
	@Inject
	private Customers customers;
	@Inject
	private Salesmen salesmen;
	@Inject
	private Otkupi otkupi;
	
	public Collection<Jewelry> getJewelrys() {
		return jewelrys.retrive();
	}

	public Collection<Customer> getCustomers() {
		return customers.retrieve();
	}

	public Collection<Salesman> geSalesmen() {
		return salesmen.retrieve();
	}

	@OnEvent(value = "submit", component = "otkupiInputForm")
	Object onFromSubmit() {

		Jewelry jewelry = null;
		Customer customer = null;
		Salesman salesman = null;

		try {
			customer = customers.retrieveSingleCustomer(getOtkupiCustomer());
			salesman= salesmen
					.retrieveSingleSalesmen(getOtkupiSalesman());
			jewelry = jewelrys.retriveSingleJewelry(getOtkupiJewelry());
		} catch (Exception ex) {
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}
		if (customer == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Customer u Map-i");
			return viewErrorPage;
		}
		if (salesman == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Salesman u Map-i");
			return viewErrorPage;
		}
		if (jewelry == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Jewelry u Map-i");
			return viewErrorPage;
		}

		Otkup otkupBean = new OtkupBean();
		otkupBean.executeOtkupCreation(salesman,customer, jewelry, otkupi);

		this.otkupiSalesman = "";
		this.otkupiCustomer = "";
		this.otkupiJewelry = "";
		
		return viewOtkupiPage;
	}

	public String getOtkupiCustomer() {
		return otkupiCustomer;
	}

	public void setOtkupiCustomer(String otkupiCustomer) {
		this.otkupiCustomer = otkupiCustomer;
	}

	public String getOtkupiSalesman() {
		return otkupiSalesman;
	}

	public void setOtkupiSalesman(String otkupiSalesman) {
		this.otkupiSalesman = otkupiSalesman;
	}

	public String getOtkupiJewelry() {
		return otkupiJewelry;
	}

	public void setOtkupiJewelry(String otkupiJewelry) {
		this.otkupiJewelry = otkupiJewelry;
	}


	
}
